package stack.medium;

import java.util.Stack;

/**
 * 每日温度
 * 示例 1:
 *
 * 输入: temperatures = [73,74,75,71,69,72,76,73]
 * 输出: [1,1,4,2,1,1,0,0]
 * 示例 2:
 *
 * 输入: temperatures = [30,40,50,60]
 * 输出: [1,1,1,0]
 * 示例 3:
 *
 * 输入: temperatures = [30,60,90]
 * 输出: [1,1,0]
 * 链接：https://leetcode-cn.com/problems/daily-temperatures
 */
public class DailyTemperatures_739 {

    public int[] dailyTemperatures(int[] temperatures) {
        int[] arr = new int[temperatures.length];
        Stack<Integer> stack = new Stack<>();
        for (int i = temperatures.length - 1; i >=0; i--) {
            while(!stack.isEmpty() && temperatures[i] >= temperatures[stack.peek()])
                stack.pop();
            if (stack.isEmpty())
                arr[i] = 0;
            else
                arr[i] = stack.peek() - i;
            stack.push(i);
        }
        return arr;
    }
}
